package com.smedi.ismedi.inmail.core.domain.aggregatemodel;

import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.smedi.ismedi.authority.starter.common.Constant.DEFAULT_STRING;

/**
 * 通知公告基础信息
 *
 * @author jiangqinxuan
 * &#064;date  2021/07/26
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class LoginLog {
    /**
     * id
     */
    private Long id;
    /**
     * 标题
     */
    private String ip;
    /**
     * 登陆时间
     */
    private LocalDateTime loginTime;
    /**
     * 登出时间
     */
    private LocalDateTime logoutTime;
    /**
     * 登录人
     */
    private Person loginUser;
    /**
     * 客户端类型
     */
    private String clientType;
    /**
     * 时长（分）
     */
    private Long duration;
    /**
     * 查看模块
     */
    private List<OperationLog> operationLogs;
    /**
     * 查看模块（逗号拼接查看模块）
     */
    private String operationLog;

    public Long getDuration() {
        return Duration.between(null == this.loginTime ? LocalDateTime.now() : this.loginTime,
                null == this.logoutTime ? LocalDateTime.now() : this.logoutTime).toMinutes();
    }

    public String getOperationLog() {
        if (VerifyHelper.isNotAvailable(this.operationLogs)) {
            return DEFAULT_STRING;
        }
        Set<String> operationLogs = new HashSet<>();
        for(OperationLog log:this.operationLogs){
            operationLogs.add(log.getModule());
        }
        return StringUtils.join(operationLogs, "，" );
    }
}